package org.jeecg.modules.mqtt;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.jeecg.modules.common.CommonMQTTConstant;
import org.jeecg.modules.simulator.jsonot.BeforeFiveMinPeople;
import org.jeecg.modules.simulator.recorder.StationInfoVo;
import org.jeecg.modules.simulator.vo.LineCurInfoVo;
import org.jeecg.modules.simulator.vo.NetCurInfoVo;
import org.jeecg.modules.simulator.vo.SectCurInfoVo;
import org.jeecg.modules.simulator.vo.TrainCurInfoVo;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

/**
 * @program: simulator-b20240918
 * @description:
 * @author: renzhg
 * @create: 2025-02-07 13:55
 **/
public class MQTTSend {


    /**
     * @description: 当前时间发送用于调整数据
     *
     * @author:  renzhg 
     * @date:  2025/2/7 14:24
     **/
    public void sendCurrentTime(String now, String sceneCode) {
        String jsonResult = "{'now':'" + now + "';" + "'sceneCode':'" + sceneCode + "'}";
        try {
            MQTTUtils.send(CommonMQTTConstant.SIMTIME, jsonResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * @description: 进度条数据
     *
     * @author:  renzhg
     * @date:  2025/2/7 14:45
     **/
    public void sendSimSchedule(String scheduleString, String nowTimeString) {
        String jsonResult = "{'scheduleString':'" + scheduleString + "';" + "'nowTimeString':'" + nowTimeString + "'}";
        try {
            MQTTUtils.send(CommonMQTTConstant.SIMSCHEDULE, jsonResult);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

/**
 * @description: 列车运行信息
 *
 * @author:  renzhg
 * @date:  2025/2/7 14:23
 **/
    public void sendSimtrainRunInfo(LocalTime startTime, LocalTime localTime, List<TrainCurInfoVo> trainCurInfoVos) {
        if (startTime.isBefore(localTime)) { // 检查开始时间是否在当前时间之前
            Gson gson = new GsonBuilder().setPrettyPrinting().create(); // 创建Gson对象
            String jsonResult = gson.toJson(trainCurInfoVos); // 将trainCurInfoVos转换为JSON字符串
            try {
                if (trainCurInfoVos != null) { // 如果trainCurInfoVos不为空
                    MQTTUtils.send(CommonMQTTConstant.SIMTRAINRUNINFO, jsonResult); // 发送JSON数据
                }
            } catch (Exception e) { // 捕获异常
                e.printStackTrace(); // 打印异常信息
            }
        }
    }
    public void sendSectCurrentTime(LocalTime startTime, LocalTime localTime,List<SectCurInfoVo> sectCurInfoVoList) {
        if(startTime.isBefore(localTime))  {
            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            String jsonResult = gson.toJson(sectCurInfoVoList);
            try {
                if (jsonResult != null) {
                    MQTTUtils.send(CommonMQTTConstant.SIMSECTINFO, jsonResult);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }


    public void sendNetCurInfo(LocalTime startTime, LocalTime localTime, NetCurInfoVo netCurInfoVo){
        if(startTime.isBefore(localTime))  {

            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            String jsonResult = gson.toJson(netCurInfoVo);

            try {
                if (jsonResult != null) {
                    MQTTUtils.send(CommonMQTTConstant.SIMNETRUNINFO, jsonResult);
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }



    /**
     * @description:过去5分钟的数据
     *
     * @author:  renzhg
     * @date:  2025/2/7 14:23
     **/
    public void passFiveSimStationInfo(LocalTime startTime, LocalTime localTime, List<BeforeFiveMinPeople> bfmplist) {
        if (startTime.isBefore(localTime)) {
            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            String BFMPjsonResult = gson.toJson(bfmplist);
            try {
                if (BFMPjsonResult != null) {
                    MQTTUtils.send(CommonMQTTConstant.PASSTFIVESIMSTATIONINFO, BFMPjsonResult);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    public void sendSimLineInfo(LocalTime startTime, LocalTime localTime, List<LineCurInfoVo> lineCurInfoVoList1) {
        if (startTime.isBefore(localTime)) {
            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            String jsonResult = gson.toJson(lineCurInfoVoList1);
            try {
                if (lineCurInfoVoList1 != null) {
                    MQTTUtils.send(CommonMQTTConstant.SIMLINEINFO, jsonResult);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}
